| Conditions | 4 |
| Paths | 32 |
| Total Lines | 117 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 4 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | |||
| 17 | class ContactsAdminSettings |
||
| 18 | { |
||
| 19 | constructor() { |
||
| 20 | this.defautOptionsAfterRender = defautOptionsAfterRender; |
||
| 21 | this.enableContacts = ko.observable(!!settingsGet('ContactsEnable')); |
||
| 22 | this.contactsSharing = ko.observable(!!settingsGet('ContactsSharing')); |
||
| 23 | this.contactsSync = ko.observable(!!settingsGet('ContactsSync')); |
||
| 24 | |||
| 25 | const |
||
| 26 | supportedTypes = [], |
||
| 27 | types = ['sqlite', 'mysql', 'pgsql'], |
||
| 28 | getTypeName = (name) => { |
||
| 29 | switch (name) |
||
| 30 | { |
||
| 31 | case 'sqlite': |
||
| 32 | name = 'SQLite'; |
||
| 33 | break; |
||
| 34 | case 'mysql': |
||
| 35 | name = 'MySQL'; |
||
| 36 | break; |
||
| 37 | case 'pgsql': |
||
| 38 | name = 'PostgreSQL'; |
||
| 39 | break; |
||
| 40 | // no default |
||
| 41 | } |
||
| 42 | |||
| 43 | return name; |
||
| 44 | }; |
||
| 45 | |||
| 46 | if (settingsGet('SQLiteIsSupported')) |
||
| 47 | { |
||
| 48 | supportedTypes.push('sqlite'); |
||
| 49 | } |
||
| 50 | if (settingsGet('MySqlIsSupported')) |
||
| 51 | { |
||
| 52 | supportedTypes.push('mysql'); |
||
| 53 | } |
||
| 54 | if (settingsGet('PostgreSqlIsSupported')) |
||
| 55 | { |
||
| 56 | supportedTypes.push('pgsql'); |
||
| 57 | } |
||
| 58 | |||
| 59 | this.contactsSupported = 0 < supportedTypes.length; |
||
| 60 | |||
| 61 | this.contactsTypes = ko.observableArray([]); |
||
| 62 | this.contactsTypesOptions = this.contactsTypes.map((value) => { |
||
| 63 | const disabled = -1 === inArray(value, supportedTypes); |
||
| 64 | return { |
||
| 65 | 'id': value, |
||
| 66 | 'name': getTypeName(value) + (disabled ? ' (' + i18n('HINTS/NOT_SUPPORTED') + ')' : ''), |
||
| 67 | 'disabled': disabled |
||
| 68 | }; |
||
| 69 | }); |
||
| 70 | |||
| 71 | this.contactsTypes(types); |
||
| 72 | this.contactsType = ko.observable(''); |
||
| 73 | |||
| 74 | this.mainContactsType = ko.computed({ |
||
| 75 | read: this.contactsType, |
||
| 76 | write: (value) => { |
||
| 77 | if (value !== this.contactsType()) |
||
| 78 | { |
||
| 79 | if (-1 < inArray(value, supportedTypes)) |
||
| 80 | { |
||
| 81 | this.contactsType(value); |
||
| 82 | } |
||
| 83 | else if (0 < supportedTypes.length) |
||
| 84 | { |
||
| 85 | this.contactsType(''); |
||
| 86 | } |
||
| 87 | } |
||
| 88 | else |
||
| 89 | { |
||
| 90 | this.contactsType.valueHasMutated(); |
||
| 91 | } |
||
| 92 | } |
||
| 93 | }).extend({notify: 'always'}); |
||
| 94 | |||
| 95 | this.contactsType.subscribe(() => { |
||
| 96 | this.testContactsSuccess(false); |
||
| 97 | this.testContactsError(false); |
||
| 98 | this.testContactsErrorMessage(''); |
||
| 99 | }); |
||
| 100 | |||
| 101 | this.pdoDsn = ko.observable(settingsGet('ContactsPdoDsn')); |
||
| 102 | this.pdoUser = ko.observable(settingsGet('ContactsPdoUser')); |
||
| 103 | this.pdoPassword = ko.observable(settingsGet('ContactsPdoPassword')); |
||
| 104 | |||
| 105 | this.pdoDsnTrigger = ko.observable(SaveSettingsStep.Idle); |
||
| 106 | this.pdoUserTrigger = ko.observable(SaveSettingsStep.Idle); |
||
| 107 | this.pdoPasswordTrigger = ko.observable(SaveSettingsStep.Idle); |
||
| 108 | this.contactsTypeTrigger = ko.observable(SaveSettingsStep.Idle); |
||
| 109 | |||
| 110 | this.testing = ko.observable(false); |
||
| 111 | this.testContactsSuccess = ko.observable(false); |
||
| 112 | this.testContactsError = ko.observable(false); |
||
| 113 | this.testContactsErrorMessage = ko.observable(''); |
||
| 114 | |||
| 115 | this.contactsType(settingsGet('ContactsPdoType')); |
||
| 116 | |||
| 117 | this.onTestContactsResponse = _.bind(this.onTestContactsResponse, this); |
||
| 118 | } |
||
| 119 | |||
| 120 | @command((self) => '' !== self.pdoDsn() && '' !== self.pdoUser()) |
||
| 121 | testContactsCommand() { |
||
| 122 | this.testContactsSuccess(false); |
||
| 123 | this.testContactsError(false); |
||
| 124 | this.testContactsErrorMessage(''); |
||
| 125 | this.testing(true); |
||
| 126 | |||
| 127 | Remote.testContacts(this.onTestContactsResponse, { |
||
| 128 | 'ContactsPdoType': this.contactsType(), |
||
| 129 | 'ContactsPdoDsn': this.pdoDsn(), |
||
| 130 | 'ContactsPdoUser': this.pdoUser(), |
||
| 131 | 'ContactsPdoPassword': this.pdoPassword() |
||
| 132 | }); |
||
| 133 | } |
||
| 134 | |||
| 222 |